package com.jianyi.system.model.entity;

import com.jianyi.common.base.BaseModel;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

@Entity
@Table(name = "sys_resource")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class SysResource extends BaseModel {
    private Long id;
    /*//父菜单ID，一级菜单为0
    private Long parentId;*/
    //父菜单名称，一级菜单为
    private String parentName;
    //菜单名称
    private String resourceName;

    private String resourceDesc;
    //菜单URL
    private String url;
    //授权(多个用逗号分隔，如：user:list,user:create)
    private String perms;
    //类型   0：目录   1：菜单   2：按钮
    private Integer type;
    //菜单图标
    private String icon;
    //排序
    private Integer orderNum;

    //所属的角色
    private Set<SysRole> role = new HashSet<SysRole>();

    private SysResource parent;

    private Set<SysResource> children;

    @Id
    @Column(nullable = false)
    @GeneratedValue(generator = "native")
    @GenericGenerator(name = "native", strategy = "native")
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }


    /**
     * 设置：菜单名称
     */
    public void setResourceName(String resourceName) {
        this.resourceName = resourceName;
    }

    /**
     * 获取：菜单名称
     */
    @Column(name = "resource_name", length = 50)
    public String getResourceName() {
        return resourceName;
    }

    /**
     * 资源描述
     *
     * @return
     */
    @Column(name = "resource_desc")
    public String getResourceDesc() {
        return resourceDesc;
    }

    public void setResourceDesc(String resourceDesc) {
        this.resourceDesc = resourceDesc;
    }

    /**
     * 设置：菜单URL
     */
    public void setUrl(String url) {
        this.url = url;
    }

    /**
     * 获取：菜单URL
     */
    @Column(name = "url")
    public String getUrl() {
        return url;
    }

    /**
     * 设置：授权(多个用逗号分隔，如：user:list,user:create)
     */
    public void setPerms(String perms) {
        this.perms = perms;
    }

    /**
     * 获取：授权(多个用逗号分隔，如：user:list,user:create)
     */
    @Column(name = "perms")
    public String getPerms() {
        return perms;
    }

    /**
     * 设置：类型   0：目录   1：菜单   2：按钮
     */
    public void setType(Integer type) {
        this.type = type;
    }

    /**
     * 获取：类型   0：目录   1：菜单   2：按钮
     */
    @Column(name = "type")
    public Integer getType() {
        return type;
    }

    /**
     * 设置：菜单图标
     */
    public void setIcon(String icon) {
        this.icon = icon;
    }

    /**
     * 获取：菜单图标
     */
    @Column(name = "icon")
    public String getIcon() {
        return icon;
    }

    /**
     * 设置：排序
     */
    public void setOrderNum(Integer orderNum) {
        this.orderNum = orderNum;
    }

    /**
     * 获取：排序
     */
    @Column(name = "order_num")
    public Integer getOrderNum() {
        return orderNum;
    }

    @Column(name = "parent_name")
    public String getParentName() {
        return parentName;
    }

    public void setParentName(String parentName) {
        this.parentName = parentName;
    }

    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.REFRESH)
    @JoinTable(
            name = "sys_role_resource",
            joinColumns = {@JoinColumn(name = "resource_id", referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "role_id", referencedColumnName = "id")}
    )
    public Set<SysRole> getRole() {
        return role;
    }

    public void setRole(Set<SysRole> role) {
        this.role = role;
    }

    @ManyToOne
    @JoinColumn(name = "parent_id")
    public SysResource getParent() {
        return parent;
    }

    public void setParent(SysResource parent) {
        this.parent = parent;
    }

    @OneToMany(fetch = FetchType.EAGER,mappedBy = "parent",cascade = CascadeType.ALL)
    public Set<SysResource> getChildren() {
        return this.children;
    }

    public void setChildren(Set<SysResource> children) {
        this.children = children;
    }
}
